{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-01T06:27:40.555791Z",
     "iopub.status.busy": "2024-02-01T06:27:40.554786Z",
     "iopub.status.idle": "2024-02-01T06:27:41.187096Z",
     "shell.execute_reply": "2024-02-01T06:27:41.187096Z",
     "shell.execute_reply.started": "2024-02-01T06:27:40.555791Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "\n",
      "version 18.0\n"
     ]
    }
   ],
   "source": [
    "clear all\n",
    "capture log close\n",
    "\n",
    "version"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-01T06:27:41.189142Z",
     "iopub.status.busy": "2024-02-01T06:27:41.189142Z",
     "iopub.status.idle": "2024-02-01T06:27:41.909125Z",
     "shell.execute_reply": "2024-02-01T06:27:41.907876Z",
     "shell.execute_reply.started": "2024-02-01T06:27:41.189142Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(8 vars, 48 obs)\n"
     ]
    }
   ],
   "source": [
    "insheet using \"ecm.csv\", comma clear"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Inspect data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-01T06:27:41.911292Z",
     "iopub.status.busy": "2024-02-01T06:27:41.910203Z",
     "iopub.status.idle": "2024-02-01T06:27:42.560941Z",
     "shell.execute_reply": "2024-02-01T06:27:42.557976Z",
     "shell.execute_reply.started": "2024-02-01T06:27:41.911292Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "    Variable |        Obs        Mean    Std. dev.       Min        Max\n",
      "-------------+---------------------------------------------------------\n",
      "       month |          0\n",
      "n_total_ce~d |         48    8703.063    8278.096        395      29478\n",
      "n_total_we~d |         48    2348.292    2316.869        224      10598\n",
      "pct_interc~d |         48    .2797099    .1891446   .0152781   .8329114\n",
      "pct_ar~l_med |         48    .6884117    .2004862   .1518987   .9828756\n",
      "-------------+---------------------------------------------------------\n",
      "pct_ar~n_med |         48    .9788974    .0242661   .9045045          1\n",
      "pct_de~l_med |         48    .0318784    .0320325   .0017017   .1532258\n",
      "pct_de~n_med |         48    .0211026    .0242661          0   .0954955\n"
     ]
    }
   ],
   "source": [
    "summarize"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-01T06:27:42.563930Z",
     "iopub.status.busy": "2024-02-01T06:27:42.562932Z",
     "iopub.status.idle": "2024-02-01T06:27:43.289296Z",
     "shell.execute_reply": "2024-02-01T06:27:43.288203Z",
     "shell.execute_reply.started": "2024-02-01T06:27:42.563930Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "month                          pct_arrivals_central_med\n",
      "n_total_central_med            pct_arrivals_western_med\n",
      "n_total_western_med            pct_deaths_central_med\n",
      "pct_interceptions_central_med  pct_deaths_western_med\n"
     ]
    }
   ],
   "source": [
    "ds, varwidth(32)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-01T06:27:43.289296Z",
     "iopub.status.busy": "2024-02-01T06:27:43.289296Z",
     "iopub.status.idle": "2024-02-01T06:27:44.072931Z",
     "shell.execute_reply": "2024-02-01T06:27:44.072931Z",
     "shell.execute_reply.started": "2024-02-01T06:27:43.289296Z"
    }
   },
   "outputs": [],
   "source": [
    "rename n_total_central_med      n_total\n",
    "rename pct_arrivals_central_med pct_arrivals"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-01T06:27:44.072931Z",
     "iopub.status.busy": "2024-02-01T06:27:44.072931Z",
     "iopub.status.idle": "2024-02-01T06:27:44.939314Z",
     "shell.execute_reply": "2024-02-01T06:27:44.937720Z",
     "shell.execute_reply.started": "2024-02-01T06:27:44.072931Z"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "gen total_thousands = n_total/1000\n",
    "drop n_total"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Generate variables"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-01T06:27:44.939314Z",
     "iopub.status.busy": "2024-02-01T06:27:44.939314Z",
     "iopub.status.idle": "2024-02-01T06:27:45.816471Z",
     "shell.execute_reply": "2024-02-01T06:27:45.816471Z",
     "shell.execute_reply.started": "2024-02-01T06:27:44.939314Z"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "gen date = mofd(date(month, \"YMD\"))\n",
    "format date %tm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-01T06:27:45.816471Z",
     "iopub.status.busy": "2024-02-01T06:27:45.816471Z",
     "iopub.status.idle": "2024-02-01T06:27:46.689199Z",
     "shell.execute_reply": "2024-02-01T06:27:46.688194Z",
     "shell.execute_reply.started": "2024-02-01T06:27:45.816471Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Time variable: date, 2016m1 to 2019m12\n",
      "        Delta: 1 month\n"
     ]
    }
   ],
   "source": [
    "tsset date, monthly"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-01T06:27:46.689199Z",
     "iopub.status.busy": "2024-02-01T06:27:46.689199Z",
     "iopub.status.idle": "2024-02-01T06:27:47.489132Z",
     "shell.execute_reply": "2024-02-01T06:27:47.489132Z",
     "shell.execute_reply.started": "2024-02-01T06:27:46.689199Z"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "sort date"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-01T06:27:47.489132Z",
     "iopub.status.busy": "2024-02-01T06:27:47.489132Z",
     "iopub.status.idle": "2024-02-01T06:27:48.291325Z",
     "shell.execute_reply": "2024-02-01T06:27:48.291325Z",
     "shell.execute_reply.started": "2024-02-01T06:27:47.489132Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "(1 missing value generated)\n",
      "\n",
      "(1 missing value generated)\n"
     ]
    }
   ],
   "source": [
    "gen diff_total_thousands     = D1.total_thousands\n",
    "gen diff_pct_arrivals      = D1.pct_arrivals\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "tags": []
   },
   "source": [
    "# Model specification"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-01T06:27:48.291325Z",
     "iopub.status.busy": "2024-02-01T06:27:48.291325Z",
     "iopub.status.idle": "2024-02-01T06:27:49.689431Z",
     "shell.execute_reply": "2024-02-01T06:27:49.689431Z",
     "shell.execute_reply.started": "2024-02-01T06:27:48.291325Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "@@@@@@@@@@@@@@@@@@@@@@@@total_thousands@@@@@@@@@@@@@@@@@@@@@@@@@\n",
      "\n",
      "Dickey–Fuller test for unit root           Number of obs  = 47\n",
      "Variable: total_thousands                  Number of lags =  0\n",
      "\n",
      "H0: Random walk without drift, d = 0\n",
      "\n",
      "                                       Dickey–Fuller\n",
      "                   Test      -------- critical value ---------\n",
      "              statistic           1%           5%          10%\n",
      "--------------------------------------------------------------\n",
      " Z(t)            -2.068       -3.600       -2.938       -2.604\n",
      "--------------------------------------------------------------\n",
      "MacKinnon approximate p-value for Z(t) = 0.2575.\n",
      "@@@@@@@@@@@@@@@@@@@@@@@@pct_arrivals@@@@@@@@@@@@@@@@@@@@@@@@@\n",
      "\n",
      "Dickey–Fuller test for unit root           Number of obs  = 47\n",
      "Variable: pct_arrivals                     Number of lags =  0\n",
      "\n",
      "H0: Random walk without drift, d = 0\n",
      "\n",
      "                                       Dickey–Fuller\n",
      "                   Test      -------- critical value ---------\n",
      "              statistic           1%           5%          10%\n",
      "--------------------------------------------------------------\n",
      " Z(t)            -2.552       -3.600       -2.938       -2.604\n",
      "--------------------------------------------------------------\n",
      "MacKinnon approximate p-value for Z(t) = 0.1034.\n"
     ]
    }
   ],
   "source": [
    "foreach v in  total_thousands  pct_arrivals  {\n",
    "\n",
    "    di \"@@@@@@@@@@@@@@@@@@@@@@@@`v'@@@@@@@@@@@@@@@@@@@@@@@@@\"  \n",
    "    dfuller `v', lags(0)\n",
    "    \n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-01T06:27:49.689431Z",
     "iopub.status.busy": "2024-02-01T06:27:49.689431Z",
     "iopub.status.idle": "2024-02-01T06:27:50.586704Z",
     "shell.execute_reply": "2024-02-01T06:27:50.583627Z",
     "shell.execute_reply.started": "2024-02-01T06:27:49.689431Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "@@@@@@@@@@@@@@@@@@@@@@@@@diff_total_thousands@@@@@@@@@@@@@@@@@@@@@@@@\n",
      "\n",
      "Dickey–Fuller test for unit root           Number of obs  = 46\n",
      "Variable: diff_total_tho~s                 Number of lags =  0\n",
      "\n",
      "H0: Random walk without drift, d = 0\n",
      "\n",
      "                                       Dickey–Fuller\n",
      "                   Test      -------- critical value ---------\n",
      "              statistic           1%           5%          10%\n",
      "--------------------------------------------------------------\n",
      " Z(t)            -6.900       -3.607       -2.941       -2.605\n",
      "--------------------------------------------------------------\n",
      "MacKinnon approximate p-value for Z(t) = 0.0000.\n",
      "@@@@@@@@@@@@@@@@@@@@@@@@@diff_pct_arrivals@@@@@@@@@@@@@@@@@@@@@@@@\n",
      "\n",
      "Dickey–Fuller test for unit root           Number of obs  = 46\n",
      "Variable: diff_pct_arriv~s                 Number of lags =  0\n",
      "\n",
      "H0: Random walk without drift, d = 0\n",
      "\n",
      "                                       Dickey–Fuller\n",
      "                   Test      -------- critical value ---------\n",
      "              statistic           1%           5%          10%\n",
      "--------------------------------------------------------------\n",
      " Z(t)            -9.163       -3.607       -2.941       -2.605\n",
      "--------------------------------------------------------------\n",
      "MacKinnon approximate p-value for Z(t) = 0.0000.\n"
     ]
    }
   ],
   "source": [
    "foreach v in  total_thousands  pct_arrivals  {\n",
    "\n",
    "    di \"@@@@@@@@@@@@@@@@@@@@@@@@@diff_`v'@@@@@@@@@@@@@@@@@@@@@@@@\"  \n",
    "    dfuller diff_`v', lags(0)\n",
    "    \n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-01T06:27:50.588998Z",
     "iopub.status.busy": "2024-02-01T06:27:50.588998Z",
     "iopub.status.idle": "2024-02-01T06:27:51.482215Z",
     "shell.execute_reply": "2024-02-01T06:27:51.482215Z",
     "shell.execute_reply.started": "2024-02-01T06:27:50.588998Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "Engle-Granger test for cointegration                  N (1st step)  =       48\n",
      "                                                      N (test)      =       47\n",
      "------------------------------------------------------------------------------\n",
      "                  Test         1% Critical       5% Critical      10% Critical\n",
      "               Statistic           Value             Value             Value\n",
      "------------------------------------------------------------------------------\n",
      " Z(t)             -4.329            -4.140            -3.469            -3.136\n",
      "\n",
      "Critical values from MacKinnon (1990, 2010)\n",
      "\n",
      "\n",
      "Breusch–Godfrey LM test for autocorrelation\n",
      "---------------------------------------------------------------------------\n",
      "    lags(p)  |          chi2               df                 Prob > chi2\n",
      "-------------+-------------------------------------------------------------\n",
      "       1     |          0.179               1                   0.6718\n",
      "       2     |          0.253               2                   0.8814\n",
      "---------------------------------------------------------------------------\n",
      "                        H0: no serial correlation\n"
     ]
    }
   ],
   "source": [
    "egranger total_thousands pct_arrivals, lags(0) // Test for cointegration\n",
    "estat bgodfrey, lags(1 2)                    // Test autocorrelation of residual in second regression > otherwise need to incorporate lags"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# ECM"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-01T06:27:51.487446Z",
     "iopub.status.busy": "2024-02-01T06:27:51.482215Z",
     "iopub.status.idle": "2024-02-01T06:27:52.627945Z",
     "shell.execute_reply": "2024-02-01T06:27:52.626919Z",
     "shell.execute_reply.started": "2024-02-01T06:27:51.487446Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "      Source |       SS           df       MS      Number of obs   =        48\n",
      "-------------+----------------------------------   F(1, 46)        =     39.21\n",
      "       Model |  1482.15018         1  1482.15018   Prob > F        =    0.0000\n",
      "    Residual |    1738.613        46  37.7959348   R-squared       =    0.4602\n",
      "-------------+----------------------------------   Adj R-squared   =    0.4485\n",
      "       Total |  3220.76318        47  68.5268762   Root MSE        =    6.1478\n",
      "\n",
      "------------------------------------------------------------------------------\n",
      "total_thou~s | Coefficient  Std. err.      t    P>|t|     [95% conf. interval]\n",
      "-------------+----------------------------------------------------------------\n",
      "pct_arrivals |   28.00997     4.4729     6.26   0.000     19.00649    37.01345\n",
      "       _cons |  -10.57933   3.204507    -3.30   0.002    -17.02967   -4.128992\n",
      "------------------------------------------------------------------------------\n",
      "\n",
      "\n",
      "\n",
      "\n",
      "      Source |       SS           df       MS      Number of obs   =        46\n",
      "-------------+----------------------------------   F(2, 43)        =      5.37\n",
      "       Model |  238.863684         2  119.431842   Prob > F        =    0.0083\n",
      "    Residual |  957.065287        43  22.2573322   R-squared       =    0.1997\n",
      "-------------+----------------------------------   Adj R-squared   =    0.1625\n",
      "       Total |  1195.92897        45  26.5761993   Root MSE        =    4.7178\n",
      "\n",
      "------------------------------------------------------------------------------\n",
      "D.           |\n",
      "total_thou~s | Coefficient  Std. err.      t    P>|t|     [95% conf. interval]\n",
      "-------------+----------------------------------------------------------------\n",
      "       resid |\n",
      "         L1. |   -.401916   .1230488    -3.27   0.002    -.6500674   -.1537645\n",
      "             |\n",
      "pct_arrivals |\n",
      "         LD. |  -3.249109   5.476577    -0.59   0.556    -14.29368     7.79546\n",
      "             |\n",
      "       _cons |   .0602109   .6981606     0.09   0.932    -1.347764    1.468186\n",
      "------------------------------------------------------------------------------\n"
     ]
    }
   ],
   "source": [
    "regress total_thousands pct_arrivals   \n",
    "cap drop resid\n",
    "predict resid, res\n",
    "\n",
    "regress D.total_thousands L.resid LD.pct_arrivals"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Stata",
   "language": "stata",
   "name": "stata"
  },
  "language_info": {
   "codemirror_mode": "stata",
   "file_extension": ".do",
   "mimetype": "text/x-stata",
   "name": "stata",
   "version": "15.1"
  },
  "toc-autonumbering": true,
  "toc-showcode": false
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
